package com.gupao.vip.bio;


import org.apache.commons.io.IOUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TimeServerHandler implements Runnable {
    private Socket socket;

    public TimeServerHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try(BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true)){
            String currentTime = null;
            String body = null;
            while((body = in.readLine()) != null){
                System.out.println("The time server receive order : " + body);
                currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME) : "BAD ORDER";
                out.println(currentTime);
            }
        }catch(Exception e){

        }

        //关闭Socket对象
        IOUtils.closeQuietly(socket);
    }
}
